package com.example.fms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fms.dto.req.CategoriesReqDto;
import com.example.fms.dto.resp.CategoriesAttributesRespDto;
import com.example.fms.dto.resp.CategoriesRespDto;
import com.example.fms.dto.resp.GoodsRespOneDto;
import com.example.fms.dto.resp.MenusDto;
import com.example.fms.service.CategoriesAttributesService;
import com.example.fms.service.CategoriesService;
import com.example.fms.vo.ResponseBean;
import org.apache.commons.collections.BagUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Validated
@RestController
@RequestMapping("/categories")
public class CategoriesController {
    @Resource
    CategoriesService categoriesService;

    @Resource
    CategoriesAttributesService categoriesAttributesService;

    //获取商品品类
    @GetMapping()
    public ResponseBean<Objects> getCategories(@RequestParam(value = "type", required = false) String type, CategoriesReqDto req,
                                               @RequestParam(value = "pagenum", required = false) Integer pagenum,
                                               @RequestParam(value = "pagesize", required = false) Integer pagesize) {
        req.setType(type);
        if (pagenum == null && pagesize == null) {
            return ResponseBean.successMeat(categoriesService.getCategories(req));
        } else {
            return ResponseBean.successMeat(categoriesService.getCategoriesPage(type,pagenum,pagesize));
        }

    }


    //添加商品分类
    @PostMapping()
    public ResponseBean<Boolean> saveCategories(@RequestBody CategoriesReqDto req) {
        return ResponseBean.successMeat(categoriesService.saveCategories(req));
    }


    //id查询商品分类
    @GetMapping("/{id}")
    public ResponseBean<CategoriesRespDto> getCategoriesById(@PathVariable("id") Long id) {
        return ResponseBean.successMeat(categoriesService.getCategoriesById(id));
    }

    //修改商品分类
    @PutMapping("/{id}")
    public ResponseBean<Boolean> putCategoriesById(@PathVariable("id") Long id,@RequestBody CategoriesReqDto req) {
        return ResponseBean.successMeat(categoriesService.putCategoriesById(id,req));
    }

    //参数列表
    @GetMapping("/{id}/attributes")
    public ResponseBean<List<CategoriesAttributesRespDto>> getAttributes(@PathVariable("id") Long id, @RequestParam(value = "sel", required = false) String sel) {
        return ResponseBean.successMeat(categoriesAttributesService.getAttributes(id,sel));
    }


    //参数添加
    @PostMapping("/{id}/attributes")
    public ResponseBean<Boolean> saveAttributes(@PathVariable("id") Integer id,@RequestBody CategoriesReqDto req) {
        return ResponseBean.successMeat(categoriesAttributesService.saveAttributes(id,req));
    }

    //根据id查询参数
    @GetMapping("/{id}/attributes/{attrId}")
    public ResponseBean<CategoriesAttributesRespDto> getAttributesById(@PathVariable("id") Long id,
                                                                       @PathVariable("attrId") Long attrId,
                                                                       @RequestParam(value = "sel", required = false) String sel) {
        return ResponseBean.successMeat(categoriesAttributesService.getAttributesById(id,attrId,sel));
    }


    //根据id查询参数
    @PutMapping("/{id}/attributes/{attrId}")
    public ResponseBean<Boolean> putAttributesById(@PathVariable("id") Long id,
                                                                       @PathVariable("attrId") Long attrId,
                                                                       @RequestBody CategoriesReqDto req) {
        return ResponseBean.successMeat(categoriesAttributesService.putAttributesById(id,attrId,req));
    }

    //根据id删除
    @DeleteMapping("/{id}/attributes/{attrId}")
    public ResponseBean<Boolean> deleteAttributesById(@PathVariable("id") Long id,
                                                   @PathVariable("attrId") Long attrId) {
        return ResponseBean.successMeat(categoriesAttributesService.deleteAttributesById(id,attrId));
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public ResponseBean<Boolean> deleteById(@PathVariable("id") Long id) {
        return ResponseBean.successMeat(categoriesService.deleteById(id));
    }




}
